﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using __ts.Core.Utilities;
using System.Text.RegularExpressions;
using __ts.Core.Configuration;

namespace CCU.PackageBuilder
{
    public partial class PublishForm : Form
    {
        public string packagePath
        {
            get { return tbSource.Text; }
            set
            {
                tbSource.Text = value;
            }
        }
        public string id
        {
            get { return tbId.Text; }
            set
            {
                tbId.Text = value;
            }
        }
        public string version
        {
            get { return tbVersion.Text; }
            set
            {
                tbVersion.Text = value;
            }
        }
        public PublishForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PackageStore.PackageStore ps = new PackageStore.PackageStore();
            ps.Url = ConfigManager.GlobalAppSettings["PackageStore"];

            if (string.IsNullOrEmpty(ps.Url))
            {
                tbTip.Text = "Error! can't find package store url.";
                return;
            }
            ps.Url = ps.Url.Trim(new char[] { '/' }) + "/PackageStore.asmx";

            if (File.Exists(this.packagePath))
            {
                if (string.IsNullOrEmpty(tbApiKey.Text)
                    || string.IsNullOrEmpty(id)
                    || string.IsNullOrEmpty(version)
                    || string.IsNullOrEmpty(tbAuthors.Text)
                    || string.IsNullOrEmpty(tbDesc.Text))
                {
                    tbTip.Text = "Error! Please complete this form.";
                    return;
                }

                FileStream fileStream = new FileStream(this.packagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] bytes = new byte[fileStream.Length];
                fileStream.Read(bytes, 0, bytes.Length);
                fileStream.Close();

                string output = string.Empty;
                if (ps.put(tbApiKey.Text, bytes, id, version, tbDesc.Text, tbAuthors.Text, Convert.ToInt32(bytes.Length / 1024).ToString(), Util.GetMd5(this.packagePath), out output))
                {
                    tbTip.Text = "Publish ok.";
                }
                else
                {
                    tbTip.Text = output;
                }
            }
            else
            {
                tbTip.Text = "Error! Can't find package.";
            }
        }

        private void PublishForm_Load(object sender, EventArgs e)
        {
            init();
        }

        private void btnBroswer_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.packagePath = openFileDialog1.FileName;
                init();
            }
        }

        private void init()
        {
            this.id = Path.GetFileNameWithoutExtension(this.packagePath);
            Match mh = Regex.Match(id, @"^.+((?:_\d){3})$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            if (mh.Success)
            {
                version = mh.Groups[1].Value.TrimStart('_').Replace("_", ".");
            }
        }
    }
}
